package com.longxi.tianjintianbao.greedao.controller;


import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.greedao.dao.WorkDao;
import com.longxi.tianjintianbao.greedao.entity.Work;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 70339 on 2018/5/16.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class DataWorkController {

    private static WorkDao mWorkDao = MyApplication.getInstance().getDaoSession().getWorkDao();

    /**
     * 增加工作页功能
     * @param work
     * @return
     */
    public static long insertWork(Work work){
        return mWorkDao.insertOrReplace(work);
    }

    public static void insertWork(List<Work> works) {
        for (Work work : works) {
            if(findWorkById(work) == null){
                insertWork(work);
            }
        }
    }

    /**
     * 查询是否有该数据
     * @param works
     * @return
     */
    public static Work findWorkById(Work works) {
        Work work = mWorkDao.queryBuilder().where(WorkDao.Properties.Id.eq(works.getId()))
                .unique();
        return work;
    }

    /**
     * 查询所有工作模块
     * @return
     */
    public static List<Work> findWork(){
        List<Work> works = mWorkDao.queryBuilder()
                .list();
        return works;
    }

    /**
     * 根据分类查询工作模块
     * @return
     */
    public static List<Work> findWorkCategory(int category){
        List<Work> works = mWorkDao.queryBuilder()
                .where(WorkDao.Properties.Category.eq(category))
                .orderAsc(WorkDao.Properties.Sort)
                .list();
        return works;
    }

    public static List<Work> findWorkCategoryVisible(int category){
        List<Work> works = mWorkDao.queryBuilder()
                .where(WorkDao.Properties.Category.eq(category))
                .where(WorkDao.Properties.IsVisible.eq(1))
                .orderAsc(WorkDao.Properties.Sort)
                .list();
        return works;
    }

    public static void updateWorkMessage(int type, int message){
        Work work = mWorkDao.queryBuilder().where(WorkDao.Properties.Type.eq(type))
                .unique();
        work.setMessage(message);
        updateWork(work);
    }

    /**
     * 更新工作模块
     */
    public static void updateWork(Work work)
    {
        mWorkDao.update(work);
    }

    /**
     * 初始化工作模块
     */
    public static void initWork(){
        for (Work work : getWork()) {
            if(findWorkById(work) == null){
                insertWork(work);
            }
        }
    }

    /**
     * 用户退出还原工作页面模块显示
     */
    public static void replaceWork(){
        for (Work work : getWork()) {
            insertWork(work);
        }
    }

    /**
     * 更新排序字段
     * @param category
     * @param isAddOrSub
     */
    public static void updateSort(int category, boolean isAddOrSub, int newSort, int oldSort){
        List<Work> works = null;
        int storage = 0;
        if(newSort > oldSort){
            storage = oldSort;
            oldSort = newSort;
            newSort = storage;
        }

        works = mWorkDao.queryBuilder()
                .where(WorkDao.Properties.Sort.between(newSort, oldSort))
                .where(WorkDao.Properties.Category.eq(category))
                .list();

        if(works == null){
            return;
        }

        for (Work w : works) {
            if(isAddOrSub)
                w.setSort(w.getSort() + 1);
            else
                w.setSort(w.getSort() - 1);
            updateWork(w);
        }

//        String sql = "";
//        if(isAddOrSub){
//            sql ="update WORK set SORT = SORT + 1"
//                    + " where SORT >= " + newSort + " and SORT < " + oldSort + " and CATEGORY = " + category;
//        }else{
//            sql ="update WORK set SORT = SORT - 1"
//                    + " where SORT <= " + newSort + " and SORT > " + oldSort + " and CATEGORY = " + category;
//        }
//        MyApplication.mDaoSession.getDatabase().rawQuery(sql, null);
//        MyApplication.db.execSQL(sql);

    }

    /**
     * 写死 为什么写死 因为服务器没传过来
     * @return
     */
    public static List<Work> getWork(){
        List<Work> works = new ArrayList<>();
        works.add(new Work(new Long(1), 0, 0,  "", 1, 1, 1));
        works.add(new Work(new Long(2), 3, 0, "", 1, 2, 1));
//        works.add(new Work(new Long(3), 0,0, "", 1, 3, 1));
        works.add(new Work(new Long(4), 0, 0, "", 1, 4, 1));
        works.add(new Work(new Long(0), 0, 0, "", 1, 5, 1));

//        works.add(new Work(new Long(5), 0,0 , "", 1, 1, 2));
        works.add(new Work(new Long(6), 0, 0, "", 1, 2, 2));
        works.add(new Work(new Long(7), 0, 0, "", 1, 3, 2));
        works.add(new Work(new Long(8), 17,0, "", 1, 4, 2));
        works.add(new Work(new Long(9), 4, 0, "", 1, 5, 2));
        works.add(new Work(new Long(10), 5,0, "", 1, 6, 2));
        works.add(new Work(new Long(11), 0,0, "", 1, 7, 2));
        works.add(new Work(new Long(12), 0,0, "", 1, 8, 2));
        works.add(new Work(new Long(13), 0,0, "", 1, 9, 2));
        works.add(new Work(new Long(14), 0,0, "", 1, 10, 2));
        works.add(new Work(new Long(15), 0,0, "", 1, 11, 2));
        works.add(new Work(new Long(16), 0,0, "", 1, 12, 2));

//        works.add(new Work(new Long(17), 1,0, "", 1, 1, 3));
        works.add(new Work(new Long(18), 2,0, "", 1, 2, 3));
        return works;
    }
}
